package com.joplus.housekeeper.adapter;

import android.content.Intent;
import android.database.DataSetObserver;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chatuidemo.DemoApplication;
import com.hyphenate.chatuidemo.ui.ChatActivityNew;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.MainBean;
import com.joplus.housekeeper.bean.MessageAccountListBean;
import com.joplus.housekeeper.bean.XmppUser;
import com.joplus.housekeeper.ui.view.swipeMenuListview.SwipeMenuLayout;
import com.joplus.housekeeper.utils.RealmHelper;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.T;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import io.realm.OrderedRealmCollection;
import io.realm.RealmBaseAdapter;
import io.realm.RealmResults;

/**
 * Created by zisheng on 2017/9/7.
 */

public class ConvertisementAdapter extends BaseAdapter {

    private final LayoutInflater layoutInflater;

    FragmentActivity activity;
    private final SimpleDateFormat simpleDateFormat;


    public ConvertisementAdapter(FragmentActivity activity) {
        this.activity = activity;

        layoutInflater = LayoutInflater.from(activity);
        simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
    }

    List<MessageAccountListBean> data = new ArrayList<>();

    public void setData(List<MessageAccountListBean> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    ViewHolder holder;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_convertisement, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(data.get(position).getAvatar(), holder.iv);
        StringUtils.setText(holder.tv_name, data.get(position).getName());

        final byte[] decode = Base64.decode(data.get(position).getLastMessageContent(), Base64.DEFAULT);
        try {
            String content = new String(decode, "UTF-8");
            StringUtils.setText(holder.tv_last, content);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Date date = new Date(data.get(position).getUpdateDatetime());
        StringUtils.setText(holder.tv_time, simpleDateFormat.format(date));
       /* if (data.get(position).getUnRead() != 0) {
            holder.tv_unread.setText(data.get(position).getUnRead() + "");
            holder.tv_unread.setVisibility(View.VISIBLE);

        } else {
            holder.tv_unread.setVisibility(View.GONE);

        }*/
        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, ChatActivityNew.class);
                intent.putExtra("userId", data.get(position).getImId());
                intent.putExtra("user", data.get(position));
                activity.startActivity(intent);
            }
        });

        holder.bt_ontop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //   Collections.swap(xmppUsers, 0, position);
              /*  XmppUser xmppUser = data.get(position);
                RealmHelper.instance().beginTran();
                xmppUser.setLastConversationTime(System.currentTimeMillis());
                RealmHelper.instance().insertNewUser(xmppUser);

                holder.container.smoothClose();*/
                T.showShort(activity, "此功能正在开发");
                holder.container.smoothClose();
            }
        });
        holder.bt_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             /*   RealmHelper.instance().beginTran();
                //  xmppUsers.remove(position);
                XmppUser xmppUser = data.get(position);
                RealmHelper.instance().removeUser(xmppUser);
                xmppUsers.remove(position);
                holder.container.smoothClose();*/
                T.showShort(activity, "此功能正在开发");
                holder.container.smoothClose();
            }
        });
        holder.bt_tran.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                T.showShort(activity, "此功能正在开发");
                holder.container.smoothClose();
            }
        });
        return convertView;
    }

    public static class ViewHolder {

        private CircleImageView iv;
        private TextView tv_name;
        private TextView tv_last;
        private TextView tv_time;
        private TextView tv_unread;
        private Button bt_ontop;
        private Button bt_del;
        private Button bt_tran;
        private RelativeLayout rl_item;
        private com.mcxtzhang.swipemenulib.SwipeMenuLayout container;

        public ViewHolder(View view) {
            iv = (CircleImageView) view.findViewById(R.id.iv);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_last = (TextView) view.findViewById(R.id.tv_last);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_unread = (TextView) view.findViewById(R.id.tv_unread);
            bt_ontop = (Button) view.findViewById(R.id.bt_ontop);
            bt_del = (Button) view.findViewById(R.id.bt_del);
            bt_tran = (Button) view.findViewById(R.id.bt_tran);
            rl_item = (RelativeLayout) view.findViewById(R.id.rl_item);
            container = (com.mcxtzhang.swipemenulib.SwipeMenuLayout) view.findViewById(R.id.container);
        }
    }


}
